<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Upload_Widgets_Filetoolbox_Widget extends Gio_Core_Widget
{
	public function showAction()
	{
		$path = 'upload';
		$path = rawurlencode(base64_encode($path));
		$this->view->path   = $path;
		//$this->view->editor = Tomato_Config::getConfig()->web->editor;
	}
	
	public function loadAction()
	{
		$request = $this->getRequest();
		
		if (!$request->isPost()) {
			return;
		}
		
		$this->view->editor = 'ckeditor';
		
		$module     	= $request->getPost('mod');
		$widgetName     = $request->getPost('name');
		$this->view->module 	= $module;
		$this->view->widgetName = $widgetName;
		
		$pageIndex     	= $request->getPost('page');
		$pageIndex      = ($pageIndex) ? $pageIndex : 1;
		$perPage   		= 30;
		$offset    		= ($pageIndex - 1) * $perPage;
		$this->view->start  = $offset;
		$this->view->end 	 = $offset + $perPage;
		
		$sizes 	= null;
		$this->view->sizes = null;
        
		$params = $request->getPost('params');
		$json = new Services_JSON();
		$params = $json->decode($params);
		
		$path 	 = (isset($params->path)) ? $params->path : null;			
		$path 	 = ($path) ? base64_decode(rawurldecode($path)) : null;
		$path 	 = trim($path, DS);
		
		$style 	 = (isset($params->style)) ? $params->style : null;
		$style	 = ($style) ? $style : 'list';
		
		$fullPath = ROOT_DIR . DS . $path;
		
		$this->view->fullPath  = $fullPath;
		
		if (!file_exists($fullPath)) {
			$fullPath = ROOT_DIR;
			$path     = null;
		}
		$dirIterator = new DirectoryIterator($fullPath);
		$dirs = $files = array();
		foreach ($dirIterator as $obj) {
			if ($obj->isDot()) {
				continue;
			}
			$name = $obj->getFilename();
			
            if (('CVS' == $name) || ('.svn' == strtolower($name))) {
                continue;
            }
			if ($obj->isDir()) {
				$dir 			 = array();
				$dir['name'] 	 = $name;				
				$dir['size'] 	 = Gio_Core_File::formatFileSize(Gio_Core_File::getDirSize($fullPath . DS . $name));
				$dir['modified'] = null;
				$dir['is_dir']	 = true;				
				$dirs[] 		 = $dir;
			} else {
				$file 			  = array();
				$file['name'] 	  = $name;
				$file['size'] 	  = Gio_Core_File::formatFileSize(@filesize($fullPath . DS . $name));
				$file['modified'] = @filemtime($fullPath . DS . $name);
				$file['is_dir']	  = false;			
				$files[] 		  = $file;
			}
		}
		$data = array_merge($dirs, $files);
		$breakcrumb = ($path) ? explode(DS, $path) : array ('');
		
		$this->view->breakcrumb = $breakcrumb;
		
		$upDir = null;
		if ($path) {
			array_pop($breakcrumb);
			$upDir = implode(DS, $breakcrumb);
		}
		
//		$paginator   = new Zend_Paginator(new Zend_Paginator_Adapter_Array($data));
//		$paginator->setCurrentPageNumber($pageIndex)->setItemCountPerPage($perPage);
//		
//		$this->_view->paginator 	   = $paginator;
//		$this->_view->paginatorOptions = array(
//			'path'	   => '',
//			'itemLink' => "javascript: Tomato.Upload.Widgets.FileToolbox.loadWidgets('" 
//						. $this->_view->directoryBuilder()->getPathLink($path) . "', %d, '" . $style . "')",
//		);
		
		$this->view->path  = $path;
		$this->view->style = $style;
		$this->view->data  = $data;
		
		$viewFilePath = ROOT_DIR . DS . 'modules' . DS . $module
									 . DS . 'widgets' . DS . $widgetName;
		$response = null;
		switch ($style) {
			case 'grid':
				$response = $this->view->render($viewFilePath . DS . '_grid.phtml');
				break;
			case 'list':
			default:
				$response = $this->view->render($viewFilePath . DS . '_list.phtml');
				break;
		}
		
		$this->view->response  = $response;
	}
	
	public function configAction()
	{
		$request = $this->getRequest();
		
		$this->view->moduleName = Gio_Core_Widget::getIntance()->getModuleName();
		$this->view->widgetName = Gio_Core_Widget::getIntance()->getWidgetName();
		$this->view->widgetIndex = $request->getParam('widget_index');
	}
}